package com.example.rg1contactapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private EditText editTextName;
    private EditText editTextPhone;

    private Button buttonAdd;
    private Button buttonQuery;
    private Button buttonAll;

    private TextView textView;
    private ContactDatabaseHelper contactDatabaseHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化
        editTextName=findViewById(R.id.editTextName);
        editTextPhone=findViewById(R.id.editTextPhone);
        buttonAdd=findViewById(R.id.buttonAdd);
        buttonQuery=findViewById(R.id.buttonQuery);
        buttonAll=findViewById(R.id.buttonAll);
        textView=findViewById(R.id.textView);
        contactDatabaseHelper=new ContactDatabaseHelper(this);

        buttonAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                contactDatabaseHelper.insert(editTextName.getText().toString(),editTextPhone.getText().toString());
            }
        });

        buttonQuery.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String phone=contactDatabaseHelper.queryByName(editTextName.getText().toString());
                textView.setText(editTextName.getText().toString()+":"+phone);
                editTextPhone.setText(phone);
            }
        });

        buttonAll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String all=contactDatabaseHelper.queryAll();
                textView.setText(all);
            }
        });
    }
}
